import { api } from '@/api/internal-api/internal-api'
import { useAuthStore } from '@/stores/auth-store';

const authStore = useAuthStore()

export async function login(usernameOrPhone, password) {
	try {
		const response = await api.post('/user/login', {
			usernameOrPhone,
			password
		});

		const responseBody = response.data
		if (responseBody.code === 200) {
			localStorage.setItem('token', responseBody.data.token)
			return true
		} else {
			return false
		}
	} catch (error) {
		return false
	}
}

export async function sendVerificationCode(phone) {
	try {
		await api.post('/user/send-code', {
			phone
		})
		return true
	} catch {
		return false
	}
}

export async function register(registerForm) {
	const registerRequestBody = {
		username: registerForm.username,
		password: registerForm.password,
		phone: registerForm.phone,
		verificationCode: registerForm.verificationCode
	}
	try {
		await api.post('/user/register', registerRequestBody);
		return true
	} catch (error) {
		return false
	}
}